﻿using System.Collections.Generic;
using System.Linq;

namespace Ymatou.CommonService
{
    public static class PagedListExtension
    {
        public static PagedList<T> ToPagedList<T>(this IQueryable<T> query, int pageIndex, int pageSize)
        {
            var count = query.Count();
            var pager = new PagerInfo(pageIndex, pageSize, count);
            return query.Skip(pager.Skip).Take(pager.PageSize).ToList().AsPagedList(pager);
        }

        public static PagedList<T> ToPagedList<T>(this IQueryable<T> query, PagerInfo pagerInfo)
        {
            return ToPagedList(query, pagerInfo.PageIndex, pagerInfo.PageSize);
        }

        public static IQueryable<T> Page<T>(this IQueryable<T> query,int pageIndex,int pageSize )
        {
            var count = query.Count();
            var pager = new PagerInfo(pageIndex, pageSize, count);
            return query.Skip(pager.Skip).Take(pager.PageSize);
        }
    }
}